package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;
import com.captain.leetcode.链表.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/6 23:43
 */
public class 二叉树的所有路径257 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root.left == null && root.right == null) {
            res.add(String.valueOf(root.val));
            return res;
        }
        recur(root, res, new StringBuilder());
        return res;
    }

    public void recur(TreeNode node, List<String> res, StringBuilder builder) {
        builder.append(node.val);
        if (node.left != null || node.right != null) {
            builder.append("->");
        }
        if (node.left != null) {
            recur(node.left, res, new StringBuilder(builder));
        }
        if (node.right != null) {
            recur(node.right, res, new StringBuilder(builder));
        }
        if (node.left == null && node.right == null) {
            res.add(builder.toString());
        }
    }
}
